package com.androidy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class dbConnection {
    public static JSONArray getAblumsList(String username)
    {    	
    	String url = "http://picasaweb.google.com/data/feed/api/user/"+username+"?alt=json";
    	HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object, Execute the request
        HttpGet httpget = new HttpGet(url); 
        HttpResponse response;

        JSONObject json = new JSONObject();
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            
            if (entity != null) {
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                json=new JSONObject(result);
                instream.close();
                return json.getJSONObject("feed").getJSONArray("entry");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return null;
    }

    public static JSONArray getImageList(String jsonURL)
    {    	
        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object, Execute the request
        HttpGet httpget = new HttpGet(jsonURL); 
        HttpResponse response;

        JSONObject json = new JSONObject();
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            
            if (entity != null) {
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                json=new JSONObject(result);
                instream.close();
                return json.getJSONObject("feed").getJSONArray("entry");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
		try {
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public static Bitmap getBitmapFromURL(String src) {
	    try {
	        URL url = new URL(src);
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoInput(true);
	    	connection.connect();
	        InputStream input = connection.getInputStream();
	        Bitmap myBitmap = BitmapFactory.decodeStream(input);
	        return myBitmap;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
    }

    public static JSONArray YoutubeList(String username)
    {    	
        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet("http://gdata.youtube.com/feeds/api/users/" + username + "/uploads?alt=json");

        // Execute the request
        HttpResponse response;

        JSONObject json = new JSONObject();
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            
            if (entity != null) {
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                json = new JSONObject(result);
                instream.close();
                return json.getJSONObject("feed").getJSONArray("entry");
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
